//
//  AppDelegate+WeChat.m
//  BrushFace
//
//  Created by timer_q on 15/12/18.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "AppDelegate+WeChat.h"
#import "WeChat.h"
#import <ALBBSDK/ALBBSDK.h>
#import <CloudPushSDK/CloudPushSDK.h>
#import "NavHelper.h"
#import "BaseTabBar.h"
@implementation AppDelegate (WeChat)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    ITLogSP(@"微信category");
    [WXApi registerApp:kWXAppId];
    [self registerAPNS:application :launchOptions];
    [self init_albb];
    [self registerMsgReceive];
    
    if (launchOptions) {
        NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (userInfo) {
            switch ([[NSString stringWithFormat:@"%@",userInfo[@"type"]] integerValue]) {
                case 1:
                {
                    [NavHelper gotoAssetsRecordVC:1 andBaseType:TYPE_FINANCE andHopeId:userInfo[@"relatedid"]];
                }
                    break;
                case 2:
                {
                    [NavHelper gotoBorrowingColorVCWithHopeID:userInfo[@"relatedid"]];
                }
                    break;
                case 3:
                {
                    [NavHelper gotoAssetsRecordVC:1 andBaseType:TYPE_BORROW_OUT andHopeId:userInfo[@"relatedid"]];
                }
                    break;
                case 4:
                {
                    [NavHelper gotoAssetsRecordVC:7 andBaseType:TYPE_BORROW_OUT andHopeId:userInfo[@"relatedid"]];
                }
                    break;
                case 5:
                {
                    [NavHelper gotoAssetsRecordVC:7 andBaseType:TYPE_BORROW_IN andHopeId:userInfo[@"relatedid"]];
                }
                    break;
                case 6:
                {
                    [NavHelper gotoAssetsRecordVC:2 andBaseType:TYPE_BORROW_OUT andHopeId:userInfo[@"relatedid"]];
                }
                    break;
                case 8:
                {
                    [NavHelper gotoMeBalance];
                }
                    break;
                case 11:
                {
                    [NavHelper gotoAssetsRecordVC:4 andBaseType:TYPE_BORROW_OUT andHopeId:userInfo[@"relatedid"]];
                }
                    break;
                case 12:
                {
                    [NavHelper gotoAssetsRecordVC:5 andBaseType:TYPE_BORROW_OUT andHopeId:userInfo[@"relatedid"]];
                }
                    break;
                case 7:
                {
                    [NavHelper gotoAssetsRecordVC:2 andBaseType:TYPE_BORROW_IN andHopeId:userInfo[@"relatedid"]];
                }
                    break;
                case 9:
                {
                    [NavHelper gotoAssetsRecordVC:4 andBaseType:TYPE_BORROW_IN andHopeId:userInfo[@"relatedid"]];
                }
                    break;
                case 10:
                {
                    [NavHelper gotoAssetsRecordVC:5 andBaseType:TYPE_BORROW_IN andHopeId:userInfo[@"relatedid"]];
                }
                    break;
                default:
                    break;
            }
        }
    }
    return YES;
}


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    return [WXApi handleOpenURL:url delegate:[WeChat sharedInstance]];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    return [WXApi handleOpenURL:url delegate:[WeChat sharedInstance]];
}



- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ // 处理推送消息
    
    if ([[NSString stringWithFormat:@"%@",userInfo[@"type"]] integerValue]!=0) {
        
        //发送消息
        NSNotification *notice = [NSNotification notificationWithName:@"haveNewMessage" object:nil];
        [[NSNotificationCenter defaultCenter]postNotification:notice];
        
    }

//    switch ([[NSString stringWithFormat:@"%@",userInfo[@"type"]] integerValue]) {
//        case 1:
//        {
//            [NavHelper gotoAssetsRecordVC:1 andBaseType:TYPE_FINANCE andHopeId:userInfo[@"relatedid"]];
//        }
//            break;
//        case 2:
//        {
//            [NavHelper gotoBorrowingColorVCWithHopeID:userInfo[@"relatedid"]];
//        }
//            break;
//        case 3:
//        {
//            [NavHelper gotoAssetsRecordVC:1 andBaseType:TYPE_BORROW_OUT andHopeId:userInfo[@"relatedid"]];
//        }
//            break;
//        case 4:
//        {
//            [NavHelper gotoAssetsRecordVC:7 andBaseType:TYPE_BORROW_OUT andHopeId:userInfo[@"relatedid"]];
//        }
//            break;
//        case 5:
//        {
//            [NavHelper gotoAssetsRecordVC:7 andBaseType:TYPE_BORROW_IN andHopeId:userInfo[@"relatedid"]];
//        }
//            break;
//        case 6:
//        {
//            [NavHelper gotoAssetsRecordVC:2 andBaseType:TYPE_BORROW_OUT andHopeId:userInfo[@"relatedid"]];
//        }
//            break;
//        case 8:
//        {
//            [NavHelper gotoMeBalance];
//        }
//            break;
//        case 11:
//        {
//            [NavHelper gotoAssetsRecordVC:4 andBaseType:TYPE_BORROW_OUT andHopeId:userInfo[@"relatedid"]];
//        }
//            break;
//        case 12:
//        {
//            [NavHelper gotoAssetsRecordVC:5 andBaseType:TYPE_BORROW_OUT andHopeId:userInfo[@"relatedid"]];
//        }
//            break;
//        case 7:
//        {
//            [NavHelper gotoAssetsRecordVC:2 andBaseType:TYPE_BORROW_IN andHopeId:userInfo[@"relatedid"]];
//        }
//            break;
//        case 9:
//        {
//            [NavHelper gotoAssetsRecordVC:4 andBaseType:TYPE_BORROW_IN andHopeId:userInfo[@"relatedid"]];
//        }
//            break;
//        case 10:
//        {
//            [NavHelper gotoAssetsRecordVC:5 andBaseType:TYPE_BORROW_IN andHopeId:userInfo[@"relatedid"]];
//        }
//            break;
//        default:
//            break;
//    }
    [CloudPushSDK handleReceiveRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error {
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error);
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    [CloudPushSDK registerDevice:deviceToken];
    NSLog(@"%@",deviceToken);//这里的Token就是我们设备要告诉服务端的Token码
}



- (void)init_albb{
    //NSString *str = [CloudPushSDK getDeviceId];;
    //ITLogSP(@"推送 deviceID ：%@",str);
    [[ALBBSDK sharedInstance] setALBBSDKEnvironment:ALBBSDKEnvironmentRelease];// 线上请选择release环境
    //sdk初始化
    
    //推送测试
     [[ALBBSDK sharedInstance] asyncInit:@"23304334" appSecret:@"ecfc62046affd22a9b81bfbf9d87136e" :^{
     NSLog(@"初始化成功");
     } failedCallback:^(NSError *error) {
     NSLog(@"初始化失败:%@",error);
     }];
    
//    //推送正式
//    [[ALBBSDK sharedInstance] asyncInit:@"23305785" appSecret:@"97e6a316959fa03fc10df80cfe6ec3a3" :^{
//        NSLog(@"初始化成功");
//    } failedCallback:^(NSError *error) {
//        NSLog(@"初始化失败:%@",error);
//    }];
    
}
-(void) registerAPNS :(UIApplication *)application :(NSDictionary *)launchOptions{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        // iOS 8 Notifications
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [application registerForRemoteNotifications];
    }
    else
    {
        // iOS < 8 Notifications
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    }
    [CloudPushSDK handleLaunching:launchOptions]; // 作为 apns 消息统计
}
#pragma mark 注册接收CloudChannel 推送下来的消息
- (void) registerMsgReceive {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMessageReceived:) name:@"CCPDidReceiveMessageNotification" object:nil]; // 注册
}
// 推送下来的消息抵达的处理示例
- (void)onMessageReceived:(NSNotification *)notification {
    NSData *data = [notification object];
    NSString *message = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    // 报警提示
    if(![NSThread isMainThread])
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            BaseTabBar *tabber =  (BaseTabBar *)self.window.rootViewController;
            UITabBarItem * item=[tabber.tabBar.items objectAtIndex:2];
            //item.badgeValue=[NSString stringWithFormat:@"2"];
            NSLog(@"%@",message);
        });
    } else {
        BaseTabBar *tabber =  (BaseTabBar *)self.window.rootViewController;
        UITabBarItem * item=[tabber.tabBar.items objectAtIndex:2];
        //item.badgeValue=[NSString stringWithFormat:@"2"];
        NSLog(@"%@",message);
    }
}


@end
